package com.yaorange.jk.web.sysadmin;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Dept;
import com.yaorange.jk.entity.Role;
import com.yaorange.jk.entity.User;
import com.yaorange.jk.service.DeptService;
import com.yaorange.jk.service.RoleService;
import com.yaorange.jk.service.UserService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.utils.SysConstant;
import com.yaorange.jk.web.BaseAction;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * @author coach tam
 * @date 2017/11/1
 */
public class UserAction extends BaseAction implements ModelDriven<User> {

    /**
     * 因为模型驱动需要实现getModel，然后我们把这个对象传递给jsp 需要单独弄一个get方法。
     * 所以，我们把名字改成model，一个getModel 就搞定了。
     */
    private User model = new User();

    //分页对象
    private Pagination page = new Pagination();
    //部门列表
    private List<Dept> deptList;
    //用户列表
    private List<User> userList;
    //角色列表
    private List<Role> roleList;

    //当前用户角色字符串
    private String userRoleStr;

    //所选角色id
    private String[] roleIds;
    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     */
    public String list() {
        //根据不同的用户等级，查询出不同的数据
        User user = getUser();
//        Long degree = user.getUserInfo().getDegree();

        page = userService.findPage(page.getPageNo(), page.getPageSize(),user);


        page.setUrl("userAction_list");
        push(page);
        return "list";
    }

    /**
     * 跳转添加页面
     */
    public String tocreate() {
        deptList = deptService.findAll();
        userList = userService.findAll();
        return "tocreate";
    }

    /**
     * 添加
     */
    public String insert() {
        model.setCreateBy(getUser().getId());
        model.setCreateDept(model.getDept().getId());
        userService.save(model);
        return "rlist";
    }

    /**
     * 跳转更新页面
     * @return
     */
    public String toupdate() {
        //根据id查询部门老数据
        model = userService.findById(model.getId());
        //存入栈顶
        push(model);

        deptList = deptService.findAll();
        return "toupdate";
    }

    /**
     *更新
     */
    public String update() {
        userService.update(model);
        return "rlist";
    }

    /**
     * 批量删除
     */
    public String delete() {
        String[] ids = model.getId().split(", ");
        userService.deleteByIds(ids);
        return "rlist";
    }

    public String toview(){
        model = userService.findById(model.getId());
        push(model);
        return "toview";
    }

    public String torole(){
        //所有的角色列表
        roleList = roleService.findAll();
        //当前用户角色 字符串  "r1,r2,r3,"
        model = userService.findById(model.getId());

        push(model);
        //查询当前用户角色
        Set<Role> roleSet = model.getRoleSet();
        StringBuilder sb = new StringBuilder();
        for (Role role:roleSet) {
            sb.append(role.getName()).append(",");
        }
        userRoleStr = sb.toString();
        return "torole";
    }


    public String role(){
        userService.updateRole(model.getId(),roleIds);

        return "rlist";
    }

    public String activity(){
        model =  userService.activity(model);
        if(model==null||model.getState()==0l)
        {
            ActionContext.getContext().getValueStack().set("errorMsg","激活失败!");
        }
        return "login";
    }


    public List<Dept> getDeptList() {
        return deptList;
    }

    public void setPage(Pagination page) {
        this.page = page;
    }

    public List<User> getUserList() {
        return userList;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public String getUserRoleStr() {
        return userRoleStr;
    }

    public void setRoleIds(String[] roleIds) {
        this.roleIds = roleIds;
    }

    @Override
    public User getModel() {
        return model;
    }


}
